{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa07329d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import yfinance as yf\n",
    "import matplotlib.pyplot as plt\n",
    "import matplotlib.dates as mdates\n",
    "from matplotlib.ticker import MultipleLocator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b88bc5fc",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "def compute_drawdowns(log_returns):\n",
    "    # Calculate cumulative returns\n",
    "    cumulative_returns = np.exp(log_returns.cumsum())\n",
    "    # Calculate ongoing drawdown\n",
    "    rolling_max = cumulative_returns.cummax()\n",
    "    drawdowns = (cumulative_returns - rolling_max) / rolling_max\n",
    "    return drawdowns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "336c2914",
   "metadata": {
    "lines_to_next_cell": 1
   },
   "outputs": [],
   "source": [
    "def plot_returns(log_returns):\n",
    "    # Create figure and axis objects\n",
    "    fig, ax = plt.subplots()\n",
    "    # Calculate cumulative returns\n",
    "    cumulative_returns = np.exp(log_returns.cumsum())\n",
    "    # Calculate ongoing drawdown\n",
    "    drawdowns = compute_drawdowns(log_returns)\n",
    "    # Plotting the results\n",
    "    ax.plot(cumulative_returns - 1, label='Cumulative Returns')\n",
    "    ax.fill_between(drawdowns.index, drawdowns, label='Drawdowns', color='red', alpha=0.3)\n",
    "    # Setting x-axis major locator to each year and formatter\n",
    "    ax.xaxis.set_major_locator(mdates.YearLocator())\n",
    "    ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y'))\n",
    "    # Setting y-axis ticks every 20%\n",
    "    ax.yaxis.set_major_locator(MultipleLocator(0.2))\n",
    "    # Adding grid with vertical lines for each year\n",
    "    ax.grid(True, which='major', linestyle='--', color='grey')\n",
    "    # Rotate x-axis labels by 45 degrees\n",
    "    plt.xticks(rotation=45)\n",
    "    # Ensure date labels are visible\n",
    "    # fig.autofmt_xdate()\n",
    "    ax.legend()\n",
    "    ax.set_title('S&P500 Cumulative Returns and Drawdowns')\n",
    "    ax.set_xlabel('Date')\n",
    "    ax.set_ylabel('Returns/Drawdown')\n",
    "    fig.tight_layout()\n",
    "    plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51243836",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download S&P 500 front month futures data\n",
    "sp500_futures_prices = yf.download('ES=F')[\"Adj Close\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2409c4fa",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Calculate logarithmic returns\n",
    "log_returns = np.log(sp500_futures_prices).diff()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9abf2cdc",
   "metadata": {},
   "outputs": [],
   "source": [
    "plot_returns(log_returns)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
